package com.katze.boot.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.katze.boot.system.dao.UriMapper;
import com.katze.boot.system.domain.SysUri;
import com.katze.boot.system.service.UriService;
import com.katze.common.Pagination;
import jakarta.annotation.PostConstruct;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.service.invoker.HttpServiceProxyFactory;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;

/**
* @author Admin
* @description 针对表【sys_uri(URI管理)】的数据库操作Service实现
* @createDate 2024-11-28 10:08:00
*/
@Service
public class UriServiceImpl extends ServiceImpl<UriMapper, SysUri> implements UriService {
    private final Map<String, HttpServiceProxyFactory> uriMap = new HashMap<>();

    @PostConstruct
    public void init() {
        LambdaUpdateWrapper<SysUri> wrapper = Wrappers.lambdaUpdate(SysUri.class);
        wrapper.eq(SysUri::getResourceType, "API").in(SysUri::getCategory, "ssh_process");
        List<SysUri> list = this.baseMapper.selectList(wrapper);
        if (!list.isEmpty()) {
            list.forEach(item -> {
                uriMap.put(item.getId(), createHttpServiceProxyFactory(item.getUri()));
            });
        }
    }


    @Override
    public <T> T getHttpServiceProxyFactory(String id, Class<T> c) {
        HttpServiceProxyFactory factory = uriMap.get(id);
        if (factory != null) {
            return factory.createClient(c);
        } else {
            throw new NoSuchElementException("请求失败, 可能服务尚未启动或者请求尚未加载");
        }
    }

    @Override
    public IPage<SysUri> findUris(Pagination pagination, SysUri entity) {
        LambdaUpdateWrapper<SysUri> wrapper = Wrappers.lambdaUpdate(SysUri.class);
        wrapper.eq(StringUtils.isNotBlank(entity.getCategory()), SysUri::getCategory, entity.getCategory());
        wrapper.like(StringUtils.isNotBlank(entity.getUri()), SysUri::getUri, entity.getUri());
        wrapper.like(StringUtils.isNotBlank(entity.getDescription()), SysUri::getDescription, entity.getDescription());
        return this.baseMapper.selectPage(pagination.toPage(OrderItem.desc("category"), OrderItem.desc("create_time")), wrapper);
    }

    @Override
    public List<SysUri> findUrisByCategory(String category) {
        LambdaUpdateWrapper<SysUri> wrapper = Wrappers.lambdaUpdate(SysUri.class);
        wrapper.likeRight(StringUtils.isNotBlank(category), SysUri::getCategory, category);
        wrapper.eq(SysUri::getStatus, "1");
        return this.baseMapper.selectList(wrapper);
    }

    @Override
    public boolean save(SysUri entity) {
        boolean rs = super.save(entity);
        if (rs && entity.getResourceType().equals("API")) {
            uriMap.put(entity.getId(), createHttpServiceProxyFactory(entity.getUri()));
        }
        return rs;
    }

    @Override
    public boolean updateById(SysUri entity) {
        SysUri old = getById(entity.getId());
        boolean rs = super.updateById(entity);
        if (rs && !old.getUri().equals(entity.getUri())) {
            uriMap.put(entity.getId(), createHttpServiceProxyFactory(entity.getUri()));
        }
        return rs;
    }

    @Override
    public boolean removeById(Serializable id) {
        boolean rs = super.removeById(id);
        if (rs) {
            uriMap.remove(id);
        }
        return rs;
    }
}




